#include "ui/CustomSplitter.h"
#include <QPainter>
#include <QStyleOption>

CustomSplitterHandle::CustomSplitterHandle(Qt::Orientation orientation, QSplitter *parent)
    : QSplitterHandle(orientation, parent)
{
    setMouseTracking(true);
    // 窄小把手尺寸
    if (orientation == Qt::Horizontal) {
        setFixedWidth(8);
    } else {
        setFixedHeight(8);
    }
}

void CustomSplitterHandle::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing, true);

    // 背景透明
    p.fillRect(rect(), Qt::transparent);

    // 小滑块形状（圆角矩形），在中间
    QColor knobColor = m_hovered ? QColor(102, 126, 234) : QColor(203, 213, 224);

    if (orientation() == Qt::Horizontal) {
        // 垂直分割：画一个竖向小圆角条
        int w = 4;
        int h = 24;
        QRect r((width() - w) / 2, (height() - h) / 2, w, h);
        p.setPen(Qt::NoPen);
        p.setBrush(knobColor);
        p.drawRoundedRect(r, 2, 2);
    } else {
        // 水平分割：画一个横向小圆角条
        int w = 24;
        int h = 4;
        QRect r((width() - w) / 2, (height() - h) / 2, w, h);
        p.setPen(Qt::NoPen);
        p.setBrush(knobColor);
        p.drawRoundedRect(r, 2, 2);
    }
}

void CustomSplitterHandle::enterEvent(
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QEvent *event
#else
    QEnterEvent *event
#endif
)
{
    QSplitterHandle::enterEvent(event);
    m_hovered = true;
    update();
}

void CustomSplitterHandle::leaveEvent(QEvent *event)
{
    QSplitterHandle::leaveEvent(event);
    m_hovered = false;
    update();
}

CustomSplitter::CustomSplitter(Qt::Orientation orientation, QWidget *parent)
    : QSplitter(orientation, parent)
{
    setHandleWidth(8);
}

QSplitterHandle *CustomSplitter::createHandle()
{
    return new CustomSplitterHandle(orientation(), this);
}


